package com.sky.service.impl;

import com.sky.mapper.FileMapper;
import com.sky.po.CompleteEx;
import com.sky.po.ExcelVo;
import com.sky.service.FileService;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class FileServiceImpl implements FileService {

    @Autowired
    private FileMapper fileMapper;


    @Override
    public List<ExcelVo> importExcel(MultipartFile file) throws Exception {
        byte[] bytes = file.getBytes();
        // 读取excel文件
        List<ExcelVo> excelList = new ArrayList<>();
        Workbook workbook = new XSSFWorkbook(new ByteArrayInputStream(bytes));
        Sheet sheet = workbook.getSheetAt(0);
        for (int i = 1; i <= sheet.getLastRowNum(); i++) {
            Row row = sheet.getRow(i);
            ExcelVo excelVo = new ExcelVo();
            excelVo.setQname(new DataFormatter().formatCellValue(row.getCell(0)));
            excelVo.setQposition(new DataFormatter().formatCellValue(row.getCell(1)));
            excelVo.setNumber(new DataFormatter().formatCellValue(row.getCell(2)));
            excelVo.setDate(new DataFormatter().formatCellValue(row.getCell(3)));
            excelVo.setFname(new DataFormatter().formatCellValue(row.getCell(4)));
            excelVo.setFtype(new DataFormatter().formatCellValue(row.getCell(5)));
            excelVo.setAdress(new DataFormatter().formatCellValue(row.getCell(6)));
            excelVo.setPrice(new DataFormatter().formatCellValue(row.getCell(7)));
            excelVo.setLperson(new DataFormatter().formatCellValue(row.getCell(8)));
            excelVo.setCperson(new DataFormatter().formatCellValue(row.getCell(9)));
            excelVo.setCposition(new DataFormatter().formatCellValue(row.getCell(10)));
            excelVo.setCphone(new DataFormatter().formatCellValue(row.getCell(11)));
            excelVo.setRperson(new DataFormatter().formatCellValue(row.getCell(12)));
            excelVo.setRphone(new DataFormatter().formatCellValue(row.getCell(13)));
            excelList.add(excelVo);
        }
        return excelList;
    }

    @Override
    public void daorumany(CompleteEx completeEx) {
            fileMapper.daoruMany(completeEx);

    }

    @Override
    public List<CompleteEx> smallfileter(String status,String qname, List<String> qpositionList) {
        List<CompleteEx> list=fileMapper.smallFilter(status,qname,qpositionList);
        return list;
    }

    @Override
    public List<CompleteEx> getByStatus(Integer status) {
        List<CompleteEx> list=fileMapper.getByStatus(status);
        return list;
    }


}

